package me.itsoo.artemis.framework.repository.executor;

import lombok.Data;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * BoundSql
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/15
 */
@Data
public class BoundSql {

    /**
     * 预编译执行 SQL
     */
    private String sql;

    /**
     * 预编译 SQL 参数
     */
    private Object[] args;

    public BoundSql(String sql) {
        this.sql = sql;
        this.args = new Object[0];
    }

    public BoundSql(String sql, Object[] args) {
        this.sql = sql;
        this.args = args;
    }

    /**
     * 创建 BoundSql
     *
     * @param sql String
     * @return BoundSql
     */
    public static BoundSql create(String sql) {
        return new BoundSql(sql);
    }

    /**
     * 创建 BoundSql
     *
     * @param sql  String
     * @param args Object[]
     * @return BoundSql
     */
    public static BoundSql create(String sql, Object[] args) {
        return new BoundSql(sql, args);
    }

    /**
     * 填充预编译 SQL 参数
     *
     * @param ps PreparedStatement
     * @throws SQLException SQLException
     */
    public void fillPreparedStatementParams(PreparedStatement ps) throws SQLException {
        for (int i = 0, len = args.length; i < len; i++) {
            ps.setObject((i + 1), args[i]);
        }
    }
}
